******************************
* Stata preamble
******************************
capture log close
set linesize 255
set mat 1500
set more off,perm
sysdir
mata: mata mlib index
set scheme plotplainblind
global G gstats tab
global S stats(mean sd min p1 q p99 max n)

			*************************
			* Program -- Winsorize
			*************************	
			capture program drop WinStd_tb
				program define WinStd_tb
				disp "Creating Winsorized (w_) and Standardized (z_) of the following:"
				while "`1'" != "" {
					qui  {
								capture drop w_`1'
								winsor `1', gen(w_`1') p(.01)
								lab var w_`1' "w_`1'"
								capture drop z_`1'
								egen z_`1' = std(w_`1')
								lab var z_`1' "z_`1'"
					}
					disp `"    `1'"'
					macro shift 
				}
				end
				
pwd		
		
use LenderCountyYear_2008_2015.dta, clear
	
	
************************************************
* Creating Variables
************************************************
/* 	Intensive = There in base year
	Extensive = New in target year
		Conditional on lending in 2015, were you there in 2008?
*/
	egen t_Sh_LenderType_1_nloan08 = mean( Sh_LenderType_1_nloan) if year==2008, by(stcou)
		egen Sh_LenderType_1_nloan08 = mean( t_Sh_LenderType_1_nloan08) , by(stcou)
	egen t_Sh_LenderType_1_nloan15 = mean( Sh_LenderType_1_nloan) if year==2015, by(stcou)
		egen tt_Sh_LenderType_1_nloan15 = mean( t_Sh_LenderType_1_nloan15) , by(stcou)
	egen t_nloan_stcou08 = mean( nloan_stcou) if year==2008, by(stcou)
		egen tt_nloan_stcou08 = mean( t_nloan_stcou08) , by(stcou)
	egen t_nloan_stcou15 = mean( nloan_stcou) if year==2015, by(stcou)
		egen tt_nloan_stcou15 = mean( t_nloan_stcou15) , by(stcou)
	gen Gr7_LenderType_1_nloan = 100*(( (tt_Sh_LenderType_1_nloan15*tt_nloan_stcou15)/tt_nloan_stcou08)   - Sh_LenderType_1_nloan08)
		WinStd_tb Gr7_LenderType_1_nloan
		
	gen t_org_nloans_stcou_2008 = (year==2008)*nloans_org_stcou
		egen org_nloans_stcou_2008 = max(t_org_nloans_stcou_2008),by(stcou org_id_AF)
	gen t_org_nloans_stcou_2011 = (year==2011)*nloans_org_stcou
		egen org_nloans_stcou_2011 = max(t_org_nloans_stcou_2011),by(stcou org_id_AF)
	gen t_org_nloans_stcou_2015 = (year==2015)*nloans_org_stcou
		egen org_nloans_stcou_2015 = max(t_org_nloans_stcou_2015),by(stcou org_id_AF)
	drop t_* tt*
	
	* How does their footprint change?
	egen  nloan_org = sum(nloans_org_stcou),by(org_id_AF year)
	gen WithOrgpp_share_stcou = 100* nloans_org_stcou/nloan_org

	gen t_WLSh08_stcou =  WithOrgpp_share_stcou  if year==2008, 
		egen WLSh08_stcou = mean( t_WLSh08_stcou) , by(stcou org_id_AF)
	gen t_WLSh11_stcou =  WithOrgpp_share_stcou  if year==2011, 
		egen WLSh11_stcou = mean( t_WLSh11_stcou) , by(stcou org_id_AF)
	gen t_WLSh15_stcou =  WithOrgpp_share_stcou  if year==2015, 
		egen WLSh15_stcou = mean( t_WLSh15_stcou) , by(stcou org_id_AF)
	gen S7_WLSh0815_stcou = WLSh15_stcou-WLSh08_stcou
		replace S7_WLSh0815_stcou = WLSh15_stcou if mi(WLSh08_stcou)
		replace S7_WLSh0815_stcou = -WLSh08_stcou  if mi(WLSh15_stcou)
	gen S4_WLSh1115_stcou = WLSh15_stcou-WLSh11_stcou
		replace S4_WLSh1115_stcou = WLSh15_stcou if mi(WLSh11_stcou)
		replace S4_WLSh1115_stcou = -WLSh11_stcou  if mi(WLSh15_stcou)
		drop t_*
	WinStd_tb WithOrgpp_share_stcou S7_WLSh0815_stcou S4_WLSh1115_stcou	
	
****************************************************************
* Table A.4: Small Bank Reallocation: Within and Across Banks
****************************************************************
* (Within) Regulation
		eststo clear
		foreach lt in   7   {		
		global SmallOnly if LenderTypeBroad==`lt' 
		qui {
			preserve
				keep  $SmallOnly
					gen t_nloan08_stcou =  nloans_org_stcou  if year==2008, 
						egen nloan08org_stcou = mean( t_nloan08_stcou) , by(stcou org_id_AF)
					gen t_nloan15_stcou =  nloans_org_stcou  if year==2015, 
						egen nloan15org_stcou = mean( t_nloan15_stcou) , by(stcou org_id_AF)
			*Capture entry/exit with one per org*stcou		
					keep if !mi(WLSh15_stcou) | !mi(WLSh08_stcou)
					bysort org_stcou: keep if _n==1
					gen Enter =  (WLSh08_stcou==0) | (WLSh08_stcou==.)
						replace Enter=100*Enter
				eststo, prefix(wrLT`lt'_Ent2): ivreghdfe Enter (w_Gr7_LenderType_1_nloan=Sh_LenderType_1_nloan08)    , absorb( org_id_AF state_fips) cluster( org_id_AF stcou )
					estadd ysumm
					drop if Enter==1
					gen Inc = 100*(nloan15org_stcou>nloan08org_stcou) if !mi(nloan08org_stcou)
				eststo, prefix(wrLT`lt'_Inc2): ivreghdfe Inc (w_Gr7_LenderType_1_nloan=Sh_LenderType_1_nloan08)    , absorb( org_id_AF state_fips) cluster( org_id_AF stcou )
					estadd ysumm
			restore
		}
		}
* (Within) G-Fees
	foreach lt in 7 {		
	global SmallOnly if LenderTypeBroad==`lt' 
	qui {
		preserve
			keep  $SmallOnly
				gen t_nloan11_stcou =  nloans_org_stcou  if year==2011, 
					egen nloan11org_stcou = mean( t_nloan11_stcou) , by(stcou org_id_AF)
				gen t_nloan15_stcou =  nloans_org_stcou  if year==2015, 
					egen nloan15org_stcou = mean( t_nloan15_stcou) , by(stcou org_id_AF)
		*Capture entry/exit with one per org*stcou		
				keep if !mi(WLSh15_stcou) | !mi(WLSh11_stcou)
				bysort org_stcou: keep if _n==1
				gen Enter =  (WLSh11_stcou==0) | (WLSh11_stcou==.)
					replace Enter=100*Enter
			eststo, prefix(wgLT`lt'_Ent2): ivreghdfe Enter nloans_TopTenGSE_share2011    , absorb( org_id_AF state_fips) cluster( org_id_AF stcou )
				estadd ysumm
				drop if Enter==1
				gen Inc = 100*(nloan15org_stcou>nloan11org_stcou) if !mi(nloan11org_stcou)
			eststo, prefix(wgLT`lt'_Inc2): ivreghdfe Inc nloans_TopTenGSE_share2011    , absorb( org_id_AF state_fips) cluster( org_id_AF stcou )
				estadd ysumm
		restore
	}
	}
* (Across) Regulation
		preserve
		 qui {
			gen t_LC_08 = LenderTypeBroad if year==2008
			egen LC_08 = max(t_LC_08),by(org_id_AF)
			label values LC_08 LenderTypeBroad

			*Get the weighted average of the shock : (already) WLSh08_stcou
			su Sh_LenderType_1_nloan08 w_Gr7_LenderType_1_nloan WLSh08_stcou
			gen WtAve08_Big4Sh_LenderSpecific = WLSh08_stcou * Sh_LenderType_1_nloan08
			gen WtAve08_Big4Gr_LenderSpecific = WLSh08_stcou * (w_Gr7_LenderType_1_nloan/100)
			
			drop t_*
			gen t_nloan_org_08 = nloan_org if year==2008
			egen nloan_org_08 = max(t_nloan_org_08),by(org_id_AF)
			gen t_nloan_org_15 = nloan_org if year==2015
			egen nloan_org_15 = max(t_nloan_org_15),by(org_id_AF)
			
			keep if inlist(year,2008) 
			gcollapse (sum) Wt* (mean) LC_08 nloan_org_* , by(org_id_AF)
				gen Gr7 = ((nloan_org_15/nloan_org_08)-1)*100
				gen ln_Gr7 = log(nloan_org_15/nloan_org_08)
				WinStd_tb Gr7 ln_Gr7
				replace w_Gr7 = (((100+w_Gr7)/100)^(1/7)-1)*100
			gen Inc = 100*(w_Gr7>0) if !mi(w_Gr7)
			*eststo clear
			qui foreach lt in 7 {
				eststo, prefix(brLT`lt'_IV_): ivreg2 w_Gr7 (WtAve08_Big4Gr_LenderSpecific= WtAve08_Big4Sh_LenderSpecific) if LC_08==`lt', robust
					estadd ysumm
				eststo, prefix(brLT`lt'_Inc_IV_): ivreg2 Inc (WtAve08_Big4Gr_LenderSpecific= WtAve08_Big4Sh_LenderSpecific) if LC_08==`lt', robust
					estadd ysumm
			}
		 }
		restore
* (Across) G-Fees
	preserve
		qui {
		gen t_LC_11 = LenderTypeBroad if year==2011
		egen LC_11 = max(t_LC_11),by(org_id_AF)
			label values LC_11 LenderTypeBroad

		*Get the weighted average of the shock : (already) WLSh11_stcou
			su   WLSh11_stcou nloans_TopTenGSE_share2011
		gen WtAve11_TopTenSh_LenderSpecific = WLSh11_stcou * nloans_TopTenGSE_share2011/100
		
		drop t_*
		gen t_nloan_org_11 = nloan_org if year==2011
		egen nloan_org_11 = max(t_nloan_org_11),by(org_id_AF)
		gen t_nloan_org_15 = nloan_org if year==2015
		egen nloan_org_15 = max(t_nloan_org_15),by(org_id_AF)
		
		keep if inlist(year,2011) 
		gcollapse (sum) Wt* (mean) LC_11 nloan_org_* , by(org_id_AF)
			gen Gr4 = ((nloan_org_15/nloan_org_11)-1)*100
			gen ln_Gr4 = log(nloan_org_15/nloan_org_11)
			WinStd_tb Gr4 ln_Gr4
				replace w_Gr4 = (((100+w_Gr4)/100)^(1/4)-1)*100
			
		gen Inc = 100*(w_Gr4>0) if !mi(w_Gr4)
		*eststo clear
		qui foreach lt in   7   {
			eststo, prefix(bgLT`lt'_): ivreg2 w_Gr4 WtAve11_TopTenSh_LenderSpecific if LC_11==`lt', robust
				estadd ysumm
			eststo, prefix(bgLT`lt'_Inc_): ivreg2 Inc WtAve11_TopTenSh_LenderSpecific if LC_11==`lt', robust
				estadd ysumm
		}
		}
		restore
	
	****************************************************************
	* Table A.4, Panel A: Big4 Exposure and Small Bank Growth
	****************************************************************
		global REGTABLE b(%10.3f) p(%8.2f) scalars(N r2  "absvars Absorbed HDFE" "clustvar Clustered by" N_clust1 "widstat FirstStage Fstat" ymean ysd) star(* 0.10 ** 0.05 *** 0.01)   ///
			addnote("`c(username)' ran on $S_DATE $S_TIME")  varwidth(32) /*noconst compress*/ label
		noisily esttab *rLT7* , $REGTABLE mtitles title(Big4 Exposure and Small Bank Growth) noconst 

	****************************************************************
	* Table A.4, Panel B: G-fee Shocks and Individual Small Bank Growth
	****************************************************************
	noisily esttab *gLT7*  , $REGTABLE mtitles title(G-fee Shocks and Individual Small Bank Growth)  noconst


